define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));

                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);

                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            subject: function () {
                Table.api.init({
                    extend: {
                        index_url: 'subject/subject/recyclebin',
                        destroy_url: 'subject/subject/destroy',
                        restore_url: 'subject/subject/restore',
                        table: 'subject',
                    }
                });

                var SubjectTable = $("#SubjectTable");

                SubjectTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: "#SubjectToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    //渲染的数据部分
                    columns: [ //渲染的字段部分
                        { checkbox: true },
                        { field: 'id', title: 'ID', operate: false, sortable: true },
                        { field: 'title', title: __('Titles'), operate: 'LIKE' },
                        { field: 'category.name', title: __('Cateid') },
                        {
                            field: 'thumbs_text',
                            title: __('Thumbs'),
                            operate: false,
                            formatter: Table.api.formatter.image
                        },
                        { field: 'price', title: __('Price'), operate: false, sortable: true },
                        { field: 'likes_text', title: __('Likes'), operate: false },
                        {
                            field: 'createtime',
                            title: __('CreateTime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: "operate",
                            title: __('Operate'),
                            table: SubjectTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'restore',
                                    title: '恢复',
                                    icon: 'fa fa-circle-o-notch',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                    extend: "data-toggle='tooltip'",
                                    confirm: "是否确认恢复数据",
                                    success: () => SubjectTable.bootstrapTable('refresh')
                                },
                                {
                                    name: 'destroy',
                                    title: '销毁',
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.destroy_url,
                                    extend: "data-toggle='tooltip'",
                                    confirm: "是否确认真实销毁数据",
                                    success: () => SubjectTable.bootstrapTable('refresh')
                                },
                            ]
                        }
                    ]
                })

                // 为表格绑定事件
                Table.api.bindevent(SubjectTable);

                $(".btn-restore").on('click', function () {
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(SubjectTable)

                    //弹框
                    layer.confirm(
                        '是否确认恢复数据',
                        { title: "恢复提醒", btn: ['是', '否'] },
                        function (index) {
                            //发送ajax请求
                            Backend.api.ajax(
                                { url: $.fn.bootstrapTable.defaults.extend.restore_url + `?ids=${ids}` },
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    SubjectTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })

                $(".btn-destroy").on('click', function () {
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(SubjectTable)

                    //弹框
                    layer.confirm(
                        '是否确认真实删除数据',
                        { title: "真实删除标题", btn: ['是', '否'] },
                        function (index) {
                            //发送ajax请求
                            Backend.api.ajax(
                                { url: $.fn.bootstrapTable.defaults.extend.destroy_url + `?ids=${ids}` },
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    SubjectTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })
            },
            order: function () {
                Table.api.init({
                    extend: {
                        index_url: 'subject/order/recyclebin',
                        destroy_url: 'subject/order/destroy',
                        restore_url: 'subject/order/restore',
                        table: 'subject_order',
                    }
                });

                var OrderTable = $("#OrderTable");

                OrderTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: "#OrderToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    //渲染的数据部分
                    columns: [ //渲染的字段部分
                        { checkbox: true },
                        { field: 'id', title: __('ID'), sortable: false, operate: false },
                        { field: 'code', title: __('OCode'), operate: 'LIKE' },
                        { field: 'subject.title', title: __('Stitle'), operate: 'LIKE' },
                        { field: 'business.nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'total', title: __('Total'), sortable: true, operate: false },
                        { field: 'createtime', title: __('Ceatetime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: "operate",
                            title: __('Operate'),
                            table: OrderTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'restore',
                                    title: '恢复',
                                    icon: 'fa fa-circle-o-notch',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                    extend: "data-toggle='tooltip'",
                                    confirm: "是否确认恢复数据",
                                    success: () => OrderTable.bootstrapTable('refresh')
                                },
                                {
                                    name: 'destroy',
                                    title: '销毁',
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.destroy_url,
                                    extend: "data-toggle='tooltip'",
                                    confirm: "是否确认真实销毁数据",
                                    success: () => OrderTable.bootstrapTable('refresh')
                                },
                            ]
                        }
                    ]
                })

                // 为表格绑定事件
                Table.api.bindevent(OrderTable);

                $(".btn-restore").on('click', function () {
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(OrderTable)

                    //弹框
                    layer.confirm(
                        '是否确认恢复数据',
                        { title: "恢复提醒", btn: ['是', '否'] },
                        function (index) {
                            //发送ajax请求
                            Backend.api.ajax(
                                { url: $.fn.bootstrapTable.defaults.extend.restore_url + `?ids=${ids}` },
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    OrderTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })

                $(".btn-destroy").on('click', function () {
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(OrderTable)

                    //弹框
                    layer.confirm(
                        '是否确认真实删除数据',
                        { title: "真实删除标题", btn: ['是', '否'] },
                        function (index) {
                            //发送ajax请求
                            Backend.api.ajax(
                                { url: $.fn.bootstrapTable.defaults.extend.destroy_url + `?ids=${ids}` },
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    OrderTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })
            },
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"))
            },
        },
    }
    return Controller;
});